package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.*;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.service.TbItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author evader
 * @date 2020-09-22 17:54
 */
@Service

public class ItemServiceImpl implements TbItemService {

    private final static Logger log = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper tbItemDescMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    public List<TbItem> selectAll() {
        return tbItemMapper.selectByExample(null);
    }

    @Override
    public EasyUIResult getItemList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbItem> tbItemList = tbItemMapper.selectByExample(new TbItemExample());
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemList);
        long total = pageInfo.getTotal();
        return new EasyUIResult(total, tbItemList);
    }

    @Override
    @Transactional(
            isolation = Isolation.READ_COMMITTED,
            propagation = Propagation.REQUIRED
    )
    public TaotaoResult saveItem(TbItem tbItem, String desc,String paramJson) {

        try {
            long itemId = IDUtils.genItemId();
            tbItem.setId(itemId);
            tbItem.setStatus(SystemConstants.TAOTAO_ITEM_STATUS_NORMAL);
            tbItem.setCreated(new Date());
            tbItem.setUpdated(new Date());
            tbItemMapper.insert(tbItem);

            TbItemDesc tbItemDesc = new TbItemDesc();
            tbItemDesc.setItemDesc(desc);
            tbItemDesc.setCreated(new Date());
            tbItemDesc.setUpdated(new Date());
            tbItemDesc.setItemId(itemId);
            tbItemDescMapper.insert(tbItemDesc);

            TbItemParamItem itemParamItem = new TbItemParamItem();
            itemParamItem.setItemId(itemId);
            itemParamItem.setParamData(paramJson);
            itemParamItem.setCreated(new Date());
            itemParamItem.setUpdated(new Date());
            tbItemParamItemMapper.insert(itemParamItem);

            return TaotaoResult.ok();
        } catch (Exception e) {
            log.error("保存商品失败", e);
            return TaotaoResult.build(SystemConstants.TAOTAO_RESULT_STATUS_ERROR, ExceptionUtil.getStackTrace(e));
        }
    }

    @Override
    public int updateByExampleSelective(TbItem record, TbItemExample example) {
        int rows = tbItemMapper.updateByExampleSelective(record, example);
        return rows;
    }

    @Override
    public TaotaoResult operatingStatus(List<Long> ids, byte status) {
        try {
            TbItem tbItem = new TbItem();
            tbItem.setStatus(status);
            TbItemExample tbItemExample = new TbItemExample();
            tbItemExample.createCriteria().andIdIn(ids);
            tbItemMapper.updateByExampleSelective(tbItem,tbItemExample);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return TaotaoResult.ok();
    }

}
